﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace AIC_Notification
{
    class Rally
    {
        public DateTime StartDate = DateTime.MinValue;
        public DateTime MidDate = DateTime.MinValue;
        public DateTime EndDate = DateTime.MinValue;
        public int MidGoal = 0;
        public int EndGoal = 0;
    }

    static class RallyHelper
    {
        private const string RALLY_FILE_NAME = "AIC_Notification-Rally.json";

        static public int NoOfRally = 8;
        static public Rally[] Rallys = null;
        static private DateTime Today;
        static public int CurrentRally = -1;
        static private double CurrentRallyWeight = -1;

        static public void Initialize()
        {
            if (Rallys != null)
                return;

            string RallyFile = Global.InputDirectory + RALLY_FILE_NAME;
            using (StreamReader sr = new StreamReader(RallyFile))
            {
                string temp = sr.ReadToEnd();
                Rallys = JsonConvert.DeserializeObject<Rally[]>(temp);
            }

            // Set current date and current rally
            Today = Global.Now();
            // Today = new DateTime(2017, 9, 1);
            CalculateCurrentRally();
        }

        static public bool RallyGoalComplete(int Rally, string School, int Essay, double NoOfDraft)
        {
            double Goal = 0;

            // If Rally below 0, set to 0.
            if (Rally < 0)
                Rally = 0;

            // If Rally exceed Max Rally, set Rally to Max Rally.
            if (Rally > NoOfRally)
                Rally = NoOfRally;

            // Determine Goal
            if (Rally == 0)
            {
                return true;
            }
            else if (Rally == 1)
            {
                if (School.Equals("PS"))
                    Goal = 8;
                else if (School.Equals("HAL") && (Essay == 2))
                    Goal = 5;
                else
                    return false;
            }
            else
                Goal = Rallys[Rally - 1].EndGoal;

            return NoOfDraft >= Goal ? true : false;
        }

        static public int RallyStatus(int Rally, string School, int Essay, double NoOfDraft)
        {
            double NDays;
            double DDays;
            double Goal = -1;

            // Before Rally begins, no coloring; after Rally ends, all coloring.
            if (CurrentRally == 0)
                return 0;

            // If Rally exceed Max Rally, set Rally to Max Rally.
            if (Rally > NoOfRally)
                Rally = NoOfRally;

            NDays = Global.SysUtil.WorkingDaysBetween(Rallys[CurrentRally - 1].StartDate, Today).TotalDays;
            if (Today < Rallys[CurrentRally - 1].MidDate)
                DDays = Global.SysUtil.WorkingDaysBetween(Rallys[CurrentRally - 1].StartDate, Rallys[CurrentRally - 1].MidDate).TotalDays;
            else
                DDays = Global.SysUtil.WorkingDaysBetween(Rallys[CurrentRally - 1].StartDate, Rallys[CurrentRally - 1].EndDate).TotalDays;

            CurrentRallyWeight = NDays / DDays;

            // Mark color only within or before current rally
            if (Rally > CurrentRally)
                return 0;

            // Determine Goal
            if (Rally == 0)
            {
                return 0;
            }
            else if (Rally == 1)
            {
                if (Rally == CurrentRally)
                {
                    if (School.Equals("PS"))
                        Goal = Today < Rallys[CurrentRally - 1].MidDate ? 4 : 8 * CurrentRallyWeight;
                    else if (School.Equals("HAL") && (Essay == 2))
                        Goal = Today < Rallys[CurrentRally - 1].MidDate ? 2 : 5 * CurrentRallyWeight;
                    else
                        Goal = -1;
                }
                else
                {
                    if (School.Equals("PS"))
                        Goal = 8;
                    else if (School.Equals("HAL") && (Essay == 2))
                        Goal = 5;
                    else
                        Goal = -1;
                }
            }
            else
            {
                if (Rally == CurrentRally)
                    Goal = Today < Rallys[CurrentRally - 1].MidDate ? Rallys[CurrentRally - 1].MidGoal : Rallys[CurrentRally - 1].EndGoal * CurrentRallyWeight;
                else
                    Goal = Rallys[Rally - 1].EndGoal;
            }

            Goal = Math.Round(Goal, 0);

            // >= Goal = Green; Within 1 = Yellow; Everything else = Red
            if (Goal > 0)
            {
                if (NoOfDraft >= Goal)
                    return 1; // Green
                else if (NoOfDraft >= Goal - 1)
                    return 2;
                else
                    return 3;
            }
            else
                return 0;
        }

        static private void CalculateCurrentRally()
        {
            CurrentRally = 0;

            for (int i = 0; i < NoOfRally; i++)
            {
                if ((Today.Year >= Rallys[i].StartDate.Year && Today.Month >= Rallys[i].StartDate.Month && Today.Day >= Rallys[i].StartDate.Day) &&
                    (Today.Year <= Rallys[i].EndDate.Year && Today.Month <= Rallys[i].EndDate.Month && Today.Day <= Rallys[i].EndDate.Day))
                {
                    CurrentRally = i + 1;
                    return;
                }
            }

            CurrentRally = NoOfRally;
        }
    }
}
